<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>栏目设置</title>
		<link rel="stylesheet" href="__PEAR__/component/pear/css/pear.css" />
		<link rel="stylesheet" href="__PEAR__/admin/css/other/department.css"/>
	</head>
	<body class="pear-container">

		<div class="layui-row layui-col-space15">
			<div class="layui-col-md3">
				<div class="layui-card">
					<div class="layui-card-body">
						<div id="channelTreeContent" style="overflow: auto">
							<ul id="channelTree" class="dtree organizationTree"></ul>
						</div>
					</div>
				</div>
			</div>
			<div class="layui-col-md9">
				<div class="layui-card">
					<div class="layui-card-body">
						<table id="channel-table" lay-filter="channel-table"></table>
					</div>
				</div>
			</div>
		</div>

		<script type="text/html" id="channel-toolbar">
		<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>
	</script>

		<script type="text/html" id="channel-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit">
				<i class="layui-icon layui-icon-edit"></i>
			</button>
		    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove">
				<i class="layui-icon layui-icon-delete"></i>
			</button>
	    </script>
	    <script type="text/html" id="displayTpl">
  			<input type="checkbox" name="is_display" value="{{d.channel_id}}" lay-skin="switch" lay-text="是|否" lay-filter="displayOption" {{ d.is_display ==1 ? 'checked' : '' }}>
		</script>
		<script src="__STATIC__/lib/layui/layui.js"></script>
		<script src="__PEAR__/component/pear/pear.js"></script>
		<script src="__STATIC__/js/admin/common.js"></script>
		<script>
			var _parent="";
			
			layui.use(['table', 'form', 'jquery', 'dtree'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let dtree = layui.dtree;
				
				_parent='';
				
				let MODULE_PATH = "/{$MODULE_NAME}/portal/";
				let DATA_PATH = MODULE_PATH+"Jsondata/";
				let TREE_PATH=DATA_PATH+"getChannels";

				let cols = [
					[	
						{
							title: '栏目编码',
							field: 'channel_id',
							align: 'left'
						},
						{
							title: '栏目名称',
							field: 'channel_name',
							align: 'left'
						},
						{
							title: '排序',
							field: 'priority',
							edit: 'text',
							align: 'left'
						},
						{
							title: '是否显示',
							field: 'is_display',
							templet: '#displayTpl',
							align: 'left'
						},
						{
							title: '操作',
							toolbar: '#channel-bar',
							align: 'center',
							width: 130
						}
						
					]
				]
				
				var treeHeight=parent.globalHeight-25;
				var DTree = dtree.render({
					elem: "#channelTree",
					height:treeHeight,
					initLevel: "2", //默认展开层级为1
					line: true, // 有线树
					ficon: ["1", "-1"], // 设定一级图标样式。0表示方形加减图标，8表示小圆点图标
					icon: ["0", "0"], // 设定二级图标样式。0表示文件夹图标，5表示叶子图标
					method: 'get',
					url: TREE_PATH
				});

				table.render({
					elem: '#channel-table',
					url: DATA_PATH+'getChannelChild',
					height: 'full-50',
					page: false,
					cols: cols,
					skin: 'line',
					toolbar: '#channel-toolbar',
					defaultToolbar: []
				});

				// 绑定节点点击事件
				dtree.on("node(channelTree)", function(obj) {
					_parent=obj.param.nodeId;
					//layer.msg("正在获取数据");
					table.reload("channel-table",{
							where:{'parent':_parent}
					});
					
				});

				table.on('tool(channel-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(channel-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});
				
				//监听单元格编辑
			  	table.on('edit(channel-table)', function(obj){
				    var value = obj.value //得到修改后的值
				    ,data = obj.data //得到所在行所有键值
				    ,field = obj.field; //得到字段
				    //console.log(obj);
				    var reg =  /^[0-9]*$/g;
				    var selector = obj.tr.selector+' td[data-field="'+obj.field+'"] div';
		    	    var oldtext = $(selector).text();
		    	    
				    if(field=='priority'){
				    	
				    	if(!reg.test(value)){
				    		layer.alert("请输入数字");
				    	    $(obj.tr.selector + ' td[data-field="' + obj.field + '"] input').val(oldtext);
				    		return;
				    	}
				    	if(oldtext!=value&&value!=''){
				    		var channel_id=data.channel_id;
				    		//判断商品编码，并且弹出商品选择窗口
					    	$.ajax({
								url:MODULE_PATH+"channel/updatePriority",
								data:{"channel_id":channel_id,'priority':value},
								dataType: 'json',
								type: 'POST',
								success: function(result) {
									//如果不为空，则填充数据
									if(result.code){
										layer.msg("更新成功");
									}else{
										layer.msg("更新失败，请重试");
									}
								}
							})
				    	}
				    }
				});
				
				//是否显示开关切换
				form.on('switch(displayOption)', function(obj){
					    
					   	/*layer.alert(JSON.stringify(this.value), {
						      title: '当前行数据：'
					    });*/
					   
					   	var is_display=0;
					   	if(obj.elem.checked){
						   is_display=1;
					   	}
					    var channel_id=this.value;
					    
					    $.ajax({
								url:MODULE_PATH+"channel/updateDisplay",
								data:{"channel_id":channel_id,'is_display':is_display},
								dataType: 'json',
								type: 'POST',
								success: function(result) {
									//如果不为空，则填充数据
									if(result.code){
										layer.msg("更新成功");
									}else{
										layer.msg("更新失败，请重试");
									}
								}
							})
			  	});

				window.add = function() {
					
					layer.open({
						type: 2,
						title: '新增栏目',
						shade: 0.1,
						area: ['800px', '500px'],
						content: MODULE_PATH + 'channel/view/option/add/parent/'+_parent+'.html'
					});
				}

				
				window.edit = function(obj) {
					var _id=obj.data.channel_id;
					layer.open({
						type: 2,
						title: '修改栏目',
						shade: 0.1,
						area: ['800px', '500px'],
						content: MODULE_PATH + 'channel/view/option/update/channelid/'+_id+'.html'
					});
				}
				
				window.remove = function(obj) {
					
					layer.confirm('确定要删除该栏目?', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
						$.ajax({
							url: MODULE_PATH + "channel/delete/channelid/" + obj.data['channel_id'],
							dataType: 'json',
							type: 'delete',
							success: function(result) {
								layer.close(loading);
								if (result.code) {
									layer.msg(result.msg, {
										icon: 1,
										time: 1000
									}, function() {
										obj.del();
									});
									var treeHeight=getParentWinHeight($)-25;
									rebuildTree(dtree,"channelTree",TREE_PATH,treeHeight);
								} else {
									layer.msg(result.msg, {
										icon: 2,
										time: 1000
									});
								}
							}
						})
					});
				}

				
				window.refresh = function(param) {
					table.reload('channel-table');
				}
				
				//$(window).resize(function(){
					//var treeHeight=getParentWinHeight($)-25;
					//rebuildTree(dtree,"channelTree",TREE_PATH,treeHeight);
				//})
				
			})
		</script>
	</body>
</html>
